#include "devlist_data.h"

GroupTreeNode::GroupTreeNode(GroupNode* pData)
: TreeItemData<GroupNode>::TreeItemData(pData)
{
}
GroupTreeNode::~GroupTreeNode()
{
	if (m_pData) delete m_pData;
}

QVariant GroupTreeNode::data(int role, int column) const
{
    Q_UNUSED(column);

    if (!m_pData) return QVariant();

    if(role == Qt::UserRole) return QVariant::fromValue((TreeItem*)(this));
    else if(role == Qt::ToolTipRole)
    {
        QString strTip;
        if (IsStore())
        {
            strTip = QString("门店: %1").arg(m_pData->m_strGroupName);
        }
        else
        {
            strTip = QString("层级: %1").arg(m_pData->m_strGroupName);
        }
        return strTip;
    }
    return QVariant();
}

bool GroupTreeNode::IsStore() const
{
	if(m_pData) return m_pData->m_bIsStore;
	return false;	
}
bool GroupTreeNode::FilterSearch(const QString& strSearch)
{
	if (!m_pData) return false;

	return QString(m_pData->m_strGroupName).contains(strSearch, Qt::CaseInsensitive);
}
QString GroupTreeNode::GetShowName() const
{
     if(m_pData) return QString(m_pData->m_strGroupName);
     return QString("");
}
bool GroupTreeNode::GetShowCount(int &store_count, int &device_count, int &online_count)
{
	int tmp_store_count = 0;
	int tmp_device_count = 0;
	int tmp_online_count = 0;

	store_count = 0;
	device_count = 0;
	online_count = 0;		
	if (IsStore()) store_count = 1;

	int child_count = childCount();
	for (int i = 0; i < child_count; i++)
	{
		TreeItem* child = childByIndex(i);
		if (!child) continue;

		if(child->DataType() == TREE_DATA_TYPE_GROUP)
		{
            GroupTreeNode* groupNode = static_cast<GroupTreeNode *>(child);
            groupNode->GetShowCount(tmp_store_count, tmp_device_count, tmp_online_count);
			store_count += tmp_store_count;
			device_count += tmp_device_count;
			online_count += tmp_online_count;
		}
		else if(child->DataType() == TREE_DATA_TYPE_DEVICE)
		{
			device_count += 1;
			DeviceNode* device = (DeviceNode*)child->Ptr();
			if (device->m_bOnline) online_count += 1;
		}		
	}
    return online_count>0 ? true:false;
}


/////////////////////////////////////////////////////////////////////////////

DeviceTreeNode::DeviceTreeNode(DeviceNode* pData)
: TreeItemData<DeviceNode>::TreeItemData(pData)
{
}
DeviceTreeNode::~DeviceTreeNode()
{
	if (m_pData) delete m_pData;
}

QVariant DeviceTreeNode::data(int role, int column) const
{
    Q_UNUSED(column);
    if (!m_pData) return QVariant();

    if (role == Qt::UserRole) return QVariant::fromValue((TreeItem*)(this));
    else if(role == Qt::ToolTipRole)
    {
        QString strTip = QString("设备: %1").arg(m_pData->m_strShowName);
        return strTip;
    }
    return QVariant();
//	if(m_pData) return m_pData->m_strShowName;
//	return QVariant();
}

QString DeviceTreeNode::GetShowName() const
{
     if(m_pData) return QString(m_pData->m_strShowName);
     return QString("");
}
bool DeviceTreeNode::IsOnline() const
{
     if(m_pData) return m_pData->m_bOnline;
     return false;
}
bool DeviceTreeNode::FilterSearch(const QString& strSearch)
{
	if (!m_pData) return false;

	return QString(m_pData->m_strShowName).contains(strSearch, Qt::CaseInsensitive);
}


/////////////////////////////////////////////////////////////////////////////

GroupDeviceTree* GroupDeviceTree::s_pInstance = nullptr;

GroupDeviceTree::GroupDeviceTree()
: mRootItem(nullptr)
{
}
GroupDeviceTree::~GroupDeviceTree()
{
	mStoreItems.clear();
	mGroupItems.clear();	
	if (mRootItem) delete mRootItem;
}

bool GroupDeviceTree::appendGroupNode(GroupNode& gNode)
{
	if(!mRootItem)
	{
		mRootItem = new(std::nothrow) TreeItem();
		if (!mRootItem) return false;
	}

	TreeItem* parentTree = nullptr;
	if (gNode.m_dwParentGroupID == 0)
	{
		parentTree = mRootItem;
	}
	else
	{
		auto it = mGroupItems.find(QString::number(gNode.m_dwParentGroupID));
		if (it != mGroupItems.end())
		{
			parentTree = it.value();
		}
	}
	if (!parentTree) return false;

	GroupNode* pNode = new(std::nothrow) GroupNode;
	if (!pNode) return false;
	memcpy(pNode, &gNode, sizeof(GroupNode));

	TreeItem* node = new(std::nothrow) GroupTreeNode(pNode);
	if (!node) return false;

    if (!parentTree->appendChild(node))
	{
		delete node;
		return false;
	}

    mGroupItems.insert(QString::number(pNode->m_dwGroupID), node);
	if (pNode->m_bIsStore)
	{
        mStoreItems.append(node);
	}
	return true;
}
bool GroupDeviceTree::appendDeviceNode(DeviceNode& dNode)
{
	TreeItem* parentTree = nullptr;
    auto it = mGroupItems.find(QString::number(dNode.m_dwStoreId));
    if (it != mGroupItems.end())
	{
		parentTree = it.value();
	}
	if (!parentTree) return false;	

	DeviceNode* pNode = new(std::nothrow) DeviceNode;
	if (!pNode) return false;
	memcpy(pNode, &dNode, sizeof(DeviceNode));	

	TreeItem* node = new(std::nothrow) DeviceTreeNode(pNode);
	if (!node) return false;

    if (!parentTree->appendChildMap(pNode->m_strDeviceSn, node))
	{
		delete node;
		return false;
	}
	return true;
}

bool GroupDeviceTree::showSearch(const QString& strSearch)
{
	bool bFind = false;
	if (!mRootItem) return bFind;

	mRootItem->setLoopFilter();

    if (strSearch.isEmpty()) return true;

	TreeItem* tmpItem = nullptr;
	bool bStoreFind = false;
    for (int j=0; j< mStoreItems.count(); j++)
	{
		bStoreFind = false;
        tmpItem = mStoreItems.value(j);
		if (!tmpItem) continue;

		GroupTreeNode* groupNode = static_cast<GroupTreeNode *>(tmpItem);
		if (groupNode->FilterSearch(strSearch))
		{
			bStoreFind = true;
		}
		else
		{
			int child_count = tmpItem->childCount();
			for (int i = 0; i < child_count; i++)
			{
                TreeItem* child = tmpItem->childByIndex(i);
				if (!child || child->DataType() != TREE_DATA_TYPE_DEVICE) continue;

                DeviceTreeNode* deviceNode = static_cast<DeviceTreeNode *>(child);
                if (deviceNode->FilterSearch(strSearch))
				{
					bStoreFind = true;	
					child->setFilter(false);
				}
			}
		}

		if (bStoreFind)
		{
			bFind = true;
			TreeItem* pTempNode = tmpItem;
			while (pTempNode)
			{
				pTempNode->setFilter(false);
				pTempNode = pTempNode->parentItem();
			}
		}
	}
	
	return bFind;
}
